package com.secret.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.secret.core.common.Result;
import com.secret.core.constant.AESConstant;
import com.secret.dao.dto.PictureDownloadDto;
import com.secret.dao.dto.UserSelectDto;
import com.secret.dao.entity.UserLogin;
import com.secret.dao.mapper.UserLoginMapper;
import com.secret.dao.vo.UserSelectVo;
import com.secret.manager.aes.MyAESUtil;
import com.secret.manager.minio.MinioUtils;
import com.secret.manager.service.TransactionConsistency;
import com.secret.manager.service.UserLoginService;
import com.secret.manager.service.UserSelectService;
import com.secret.service.UserService;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserSelectDto userSelectDto;
    @Resource
    private UserSelectVo userSelectVo;
    @Resource
    private UserLoginMapper userLoginMapper;
    @Resource
    private UserLogin userLogin;
    @Resource
    private MinioUtils minioUtils;
    @Resource
    private UserSelectService  userSelectService;
    @Resource
    private TransactionConsistency transactionConsistency;
    @Resource
    private UserLoginService userLoginService;

    @Override
    @ApiModelProperty("用户查询图片外链接")
    public Result selectPictureList(String secret) throws Exception {
        String pojo=MyAESUtil.Decrypt(secret,AESConstant.AES_KEY_VO);
        if (Objects.isNull(pojo)) {
            return Result.fail("用户信息解密失败");
        }
        userSelectVo=JSONObject.parseObject(pojo,UserSelectVo.class);
        //使用数据库加密openid查询数据库获取userlogin对象
        userLogin=userLoginService.effect(userSelectVo.getOpenId());
        if (Objects.isNull(userLogin)) {
            return Result.fail("用户不存在");
        }
        String number=userSelectVo.getNumber();
        //查询redis
        List<PictureDownloadDto> redisList=userSelectService.redisSearch(number);
        if (!redisList.isEmpty()){
            return Result.success(redisList);
        }
        //查询mysql
        List<PictureDownloadDto> mysqlList=userSelectService.mysqlSearch(number);
        if (mysqlList.isEmpty()){
            return Result.fail("该序列不存在或已下架");
        }
        List<PictureDownloadDto> resultList=userSelectService.minioSearch(mysqlList);
        return  Result.success(resultList);
    }


}
